package org.dtrd.modules.doctor.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.doctor.entity.bean.DoctorStatisticInfoDetail;
import org.dtrd.modules.doctor.entity.po.DtrdEntRdDoctorStatisticInfo;
import org.dtrd.modules.doctor.mapper.DtrdEntRdDoctorStatisticInfoMapper;
import org.dtrd.modules.doctor.service.IDtrdEntRdDoctorStatisticInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


/**
 * <p>
 * 医生相关的统计数据 服务实现类
 * </p>
 *
 * @author QR
 * @since 2022-04-14
 */
@DS("multi-datasource1")
@Service
public class DtrdEntRdDoctorStatisticInfoServiceImpl extends ServiceImpl<DtrdEntRdDoctorStatisticInfoMapper, DtrdEntRdDoctorStatisticInfo> implements IDtrdEntRdDoctorStatisticInfoService {

    @Override
    public List<DtrdEntRdDoctorStatisticInfo> getDoctorStatisticInfoList(Integer doctorId,Integer teamId, Integer limitSize) {
        LambdaQueryWrapper<DtrdEntRdDoctorStatisticInfo> queryWrapper = baseQueryWrapper()
                .eq(doctorId != null, DtrdEntRdDoctorStatisticInfo::getDoctorId, doctorId)
                .eq(teamId !=null ,DtrdEntRdDoctorStatisticInfo::getTeamId, teamId)
                .orderByDesc(DtrdEntRdDoctorStatisticInfo::getCreateTime);
        if (limitSize != null && limitSize > 0) {
            queryWrapper.last("limit " + limitSize);
        }
        return list(queryWrapper);
    }

    @Override
    public DoctorStatisticInfoDetail getDoctorStatisticInfo(Integer dataId) {
        DtrdEntRdDoctorStatisticInfo dtrdEntRdDoctorStatisticInfo = getById(dataId);
        return Optional.ofNullable(dtrdEntRdDoctorStatisticInfo)
                .map(po -> new DoctorStatisticInfoDetail().parseFromPO(po))
                .orElse(null);
    }

    @Override
    public void removeByDoctorId(Integer doctorId) {
        baseMapper.removeByDoctorId(doctorId);
    }

    private LambdaQueryWrapper<DtrdEntRdDoctorStatisticInfo> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntRdDoctorStatisticInfo.class)
                .eq(DtrdEntRdDoctorStatisticInfo::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntRdDoctorStatisticInfo> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntRdDoctorStatisticInfo.class)
                .eq(DtrdEntRdDoctorStatisticInfo::getIsDel, 0);
    }
}
